<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table
          border
          :data="list"
          row-key="id"
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 如果type==1(页面级别的)，才显示添加，如果type=2就没有添加按钮 -->
              <el-button v-if="scope.row.type===1" type="text" @click="hAdd(2, scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 添加权限-对话框 -->
    <el-dialog
      title="添加权限"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form ref="form" :model="formData" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            inactive-text="不启用"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { tranListToTreeData } from '@/utils/index.js'
import { getPermission, addPermission, delPermission, editPermission } from '@/api/permission'
export default {
  data() {
    return {
      formData: {
        type: '',
        pid: '',
        name: '',
        code: '',
        description: '',
        enVisible: false
      },
      isEdit: false, // 当前是否是编辑
      showDialog: false,
      list: [] // 保存权限数据
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    async loadPermission() {
      const res = await getPermission()
      console.log('获取权限数据', res)

      this.list = tranListToTreeData(res.data)
    },
    // 用户点击了添加权限
    hAdd(type, pid) {
      console.log('当前要添加的权限数据', type, pid)

      // 更新参数
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true

      this.isEdit = false
    },
    async doAdd() {
      const queryObj = { ...this.formData }
      queryObj.enVisible = this.formData.enVisible ? '1' : '0'
      const res = await addPermission(queryObj)
      console.log('添加结果', res)

      this.$message.success('添加成功')
      this.showDialog = false
      this.loadPermission()
    },
    hSubmit() {
      // 表单校验
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    async doDel(id) {
      const res = await delPermission(id)
      console.log('删除权限点', res)

      this.$message.success('删除成功')
      this.loadPermission()
    },
    // 用户点击了删除
    hDel(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doEdit() {
      const queryObj = { ...this.formData }
      queryObj.enVisible = this.formData.enVisible ? '1' : '0'

      const res = await editPermission(queryObj)
      console.log('编辑权限点', res)

      this.$message.success('编辑成功')

      this.showDialog = false
      this.loadPermission()
    },
    hEdit(row) {
      // 1. 更改状态
      this.isEdit = true

      // 2. 显示弹框
      this.showDialog = true

      // 3. 数据回填
      this.formData = {
        id: row.id,
        type: row.type,
        pid: row.pid,
        name: row.name,
        code: row.code,
        description: row.description,
        enVisible: row.enVisible === '1' // "1" ---> true,  '0' ---> false
      }

      console.log(row)
    },
    // showDialog: true  --> false
    hClose() {
      // 清空表单
      // 1. 清空数据
      this.formData = {
        type: '',
        pid: '',
        name: '',
        code: '',
        description: '',
        enVisible: false
      }
      // 2. 清空表单校验结果
      this.$refs.form.resetFields()
    }
  }
}
</script>
